package com.situ.mall.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.situ.mall.api.product.ProductClient;
import com.situ.mall.common.util.Result;
import com.situ.mall.order.mapper.OrderInfoMapper;
import com.situ.mall.pojo.VO.EChartsVO;
import com.situ.mall.pojo.VO.IdVO;
import com.situ.mall.pojo.entity.order.OrderItem;
import com.situ.mall.order.mapper.OrderItemMapper;
import com.situ.mall.order.service.IOrderItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.mall.pojo.entity.product.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单项信息 服务实现类
 * </p>
 *
 * @author CKX
 * @since 2024-10-16
 */
@Service
public class OrderItemServiceImpl extends ServiceImpl<OrderItemMapper, OrderItem> implements IOrderItemService {

    @Autowired
    private OrderItemMapper orderItemMapper;
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private ProductClient productClient;

    @Override
    public Map<String, Object> selectByOrderNo(Integer id) {
        String orderNo = orderInfoMapper.selectById(id).getOrderNo();
        QueryWrapper<OrderItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", orderNo);
        List<OrderItem> list = orderItemMapper.selectList(queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        return map;
    }

    @Override
    public List<EChartsVO> selectEarning() {
        List<EChartsVO> list = new ArrayList<>();
        Result<List<IdVO>> idListResult = productClient.selectIds();
        List<IdVO> idList = new ArrayList<>();
        if (idListResult.getCode() == 0) {
            idList = idListResult.getData();
        }
        for (IdVO idVO : idList) {
            EChartsVO eChartsVO = new EChartsVO();
            QueryWrapper<OrderItem> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("product_id", idVO.getId());
            int quantity = 0;
            List<OrderItem> orderItemList = orderItemMapper.selectList(queryWrapper);
            if (!orderItemList.isEmpty()) {
                BigDecimal price = BigDecimal.valueOf(0);
                String name = null;
                for (OrderItem orderItem : orderItemList) {
                    quantity += orderItem.getQuantity();
                    price = orderItem.getUnitPrice();
                    name = orderItem.getProductName();
                }
                double earning = quantity * price.doubleValue();
                eChartsVO.setName(name);
                eChartsVO.setValue(earning);
                list.add(eChartsVO);
            } else {
                Result<Product> productResult = productClient.selectById(idVO.getId());
                Product product = productResult.getData();
                String name = product.getName();
                double earning = 0;
                eChartsVO.setName(name);
                eChartsVO.setValue(earning);
                list.add(eChartsVO);
            }
        }
        return list;
    }
}
